package cn.kailangtu.dataspot.activify;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.navigation.fragment.NavHostFragment;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.Random;
import cn.kailangtu.commonevents.eventEntity.FragmentChangedEvent;
import cn.kailangtu.commonevents.eventEntity.KeyboardStopOpenEvent;
import cn.kailangtu.commonevents.eventEntity.OnFragmentChangeEvent;
import cn.kailangtu.commonevents.eventEntity.ViewShowSwitchEvent;
import cn.kailangtu.commontools.APPSPUtils;
import cn.kailangtu.commontools.AppVariable;
import cn.kailangtu.dataspot.R;
import cn.kailangtu.dataspot.data.ControlTree;
import cn.kailangtu.dataspot.data.PageIndex;
import cn.kailangtu.dataspot.data.consts.ControlAction;
import cn.kailangtu.dataspot.data.context.SelectContext;
import cn.kailangtu.dataspot.databinding.FragmentSwitchTestBinding;
import cn.kailangtu.dataspot.status;
import cn.kailangtu.dataspot.tasks.BaseTask;

public class SwitchTestFragment extends BaseFragment {

    private FragmentSwitchTestBinding binding;
    int random =  1000 + new  Random().nextInt(10000 - 1000 + 1);
    boolean lock = false;
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState
    ) {


        //APPSPUtils.getInstance().putStopPwdRandom();
        binding = FragmentSwitchTestBinding.inflate(inflater, container, false);
        return binding.getRoot();

    }

    public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        EventBus.getDefault().post(new FragmentChangedEvent(R.layout.fragment_switch_test));

        Log.e("RANDOMRANDOMRANDOM","::"+random);
        binding.tvSwitchTestTitle7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!status.isStop)
                {
                    EventBus.getDefault().post(new KeyboardStopOpenEvent());
                }
              //  binding.rlSwitchStop.setVisibility(View.VISIBLE);
                status.isStop = true;


            }
        });
        initListener();
    }

    private void initListener(){

    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ViewShowSwitchEvent event)
    {
        if(!event.isShowDuanlu())
        {
            binding.rlSwitchHeza.setVisibility(View.GONE);
        }
        if(!event.isShowGuapai())
        {
            binding.rlSwitchGuapai.setVisibility(View.GONE);
        }
        if(!event.isShowGuozai()){
            binding.rlSwitchGuozai.setVisibility(View.GONE);
        }
        if(!event.isShowHeza()){
            binding.rlSwitchHeza.setVisibility(View.GONE);
        }
        if(!event.isShowKaiza()){
            binding.rlSwitchFenza.setVisibility(View.GONE);
        }
        if(!event.isShowLoudian()){
            binding.rlSwitchLoudian.setVisibility(View.GONE);
        }
        if(!event.isShowZijian()){
            binding.rlSwitchZijian.setVisibility(View.GONE);
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(OnFragmentChangeEvent event) {
        // Do something
        if(R.layout.fragment_factory_setting == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestFragment.this)
                    .navigate(R.id.action_SwitchTestFragment_to_FactorySettingFragment);
        }
        else if(R.layout.fragment_switch_test == event.getTargetFragmentId())
        {

        }
        else if(R.layout.fragment_common_setting == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestFragment.this)
                    .navigate(R.id.action_SwitchTestFragment_to_CommonSettingFragment);
        }
        else if(R.layout.fragment_exception_log == event.getTargetFragmentId())
        {
            NavHostFragment.findNavController(SwitchTestFragment.this)
                    .navigate(R.id.action_SwitchTestFragment_to_ExceptionLogFragment);
        }
        else if(R.layout.fragment_real_picture == event.getTargetFragmentId())
        {
            if(!status.isStop) {
                NavHostFragment.findNavController(SwitchTestFragment.this)
                        .navigate(R.id.action_SwitchTestFragment_to_RealPictureFragment);
            }
        }
    }


    @Override
    protected int processControlsEvent(int eventType) {
        if(eventType == ControlAction.Selection)
        {
            TextView textView =  this.getActivity().findViewById(controlTree.getSelectNode().getIndex());
            SelectContext.setSelectedView(textView);
        }
        else if(eventType == ControlAction.Entry){

                TextView tv = getActivity().findViewById(controlTree.getSelectNode().getIndex());
                tv.callOnClick();

        }
        return 0;
    }

    @Override
    protected void buildControlTree() {
        controlTree = ControlTree.getSwitch();
        controlTree.setSelectNode(null);
    }

    @Override
    protected void resetPageIndex() {

        if(APPSPUtils.getInstance().isStop())
        {
            status.localPageIndex = PageIndex.H_STOP;
            BaseTask.toNewPage(status.localPageIndex);
        }
        else {
            if (status.localToPlc != 2) {
                status.localPageIndex = PageIndex.H_SWITCH;
                BaseTask.toNewPage(status.localPageIndex);
            } else {
                if (status.plcPageIndex == PageIndex.H_SWITCH) {
                    status.localPageIndex = PageIndex.H_SWITCH;
                } else if (status.plcPageIndex == PageIndex.H_STOP) {
                    binding.tvSwitchTestTitle7.callOnClick();
                }
            }
        }
    }

    @Override
    void resetPlatform() {
        if(AppVariable.platform_version == 450 && AppVariable.need_reset_view) {
            commonReset(binding.rlSwitchTestContainer);
        }
    }
}